﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;

namespace Project01_DBSecurity
{
	public partial class Form_Role : Form
	{
		private int type;
		public Form_Role(int type, string rolename)
		{
			InitializeComponent();
			this.type = type;
			switch (type)
			{
				case 1: // tạo mới
					{
					}
					break;
				case 2: // sửa
					{
						textBox1.Text = rolename;
						textBox1.Enabled = false;
					}
					break;
			}
		}

		private void btOK_Click(object sender, EventArgs e)
		{
			OracleCommand comm = new OracleCommand();
			comm.Connection = orcl.conn;
			if ( type == 1 )
				comm.CommandText = "create role \"";
			else if ( type == 2 )
				comm.CommandText = "alter role \"";
			comm.CommandText += textBox1.Text + "\"";
			if (radioButton1.Checked == true) // no password
				comm.CommandText += " not identified";
			else // pass
				if (textBox3.Text != textBox4.Text)
				{
					MessageBox.Show("Password không trùng nhau", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					return;
				}
				else if (orcl.PasswordCheck(textBox3.Text) != 0)
				{
					MessageBox.Show("Password không hợp lệ", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					return;
				}
				else
					comm.CommandText += " identified by " + textBox3.Text;
			try
			{
				comm.ExecuteNonQuery();
				comm.Dispose();
				MessageBox.Show("Thêm/Sửa role thành công", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
				this.Close();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void btCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}
